﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
//using System.Web.Configuration;
using System.Configuration;
namespace LiveSSOForDotNet.Data
{
    class DataHelper
    {
        /// <summary>
        /// </summary>
        /// <returns>conection SQL Server 2005</returns>
        private SqlConnection conn = null;
        public DataHelper()
        {
            conn = new SqlConnection(GetConnectionString());
        }
        private static string GetConnectionString()
        {
            return ConfigurationManager.AppSettings["MSSQLConnectionString"];

        }
        /// <summary>
        /// run stored procedure & return 1 instance SqlDataReader
        /// </summary>
        /// <param name="procName">procedure's name</param>
        /// <param name="procParams">params list</param>
        /// <returns>1 instance SqlDataReader content storedprocedure's result </returns>
        public SqlDataReader ExecuteReader(string procName, params SqlParameter[] procParams)
        {
            //SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(GetConnectionString());
                conn.Open();
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = procName;
                cmd.CommandType = CommandType.StoredProcedure;
                if (procParams != null)
                {
                    for (int i = 0; i < procParams.Length; i++)
                    {
                        cmd.Parameters.Add(procParams[i]);
                    }
                }
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                throw;
            }
            return reader;
        }
        /// <summary>
        /// run stored procedure & return value
        /// </summary>
        /// <param name="procName">storedprocedure's name</param>
        /// <param name="procParams">params list</param>
        /// <returns>int</returns>
        public int ExecuteNonQuery(string procName, params SqlParameter[] procParams)
        {
            SqlCommand cmd = null;
            SqlConnection conn = null;
            int affectedRows = 0;
            try
            {
                conn = new SqlConnection(GetConnectionString());
                conn.Open();
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = procName;
                cmd.CommandType = CommandType.StoredProcedure;
                if (procParams != null)
                {
                    for (int i = 0; i < procParams.Length; i++)
                    {
                        cmd.Parameters.Add(procParams[i]);
                    }
                }
                affectedRows = cmd.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                conn.Close();
            }
            return affectedRows;
        }

        /// <summary>
        /// run storedprocedure & return top 1 of row record 
        /// </summary>
        /// <param name="procName">storedprocedure's name</param>
        /// <param name="procParams">params list</param>
        /// <returns>object</returns>
        public object ExecuteScalar(string procName, params SqlParameter[] procParams)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            object value;
            try
            {
                conn = new SqlConnection(GetConnectionString());
                conn.Open();
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = procName;
                cmd.CommandType = CommandType.StoredProcedure;
                if (procParams != null)
                {
                    for (int i = 0; i < procParams.Length; i++)
                    {
                        cmd.Parameters.Add(procParams[i]);
                    }
                }
                value = cmd.ExecuteScalar();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Dispose();
                }
            }
            return value;
        }
        /// <summary>
        /// run 1 stored procedure và return DataSet
        /// </summary>
        /// <param name="procName">stored procedure's name</param>
        /// <param name="procParams">params list</param>
        /// <returns>dataset</returns>
        public DataSet ExecuteDataSet(string procName, params SqlParameter[] procParams)
        {
            SqlConnection conn = null;
            SqlDataAdapter adapter = null;
            DataSet ds = new DataSet();
            SqlCommand cmd = null;
            try
            {
                conn = new SqlConnection(GetConnectionString());
                cmd = new SqlCommand(procName, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                if (procParams != null)
                {
                    for (int i = 0; i < procParams.Length; i++)
                    {
                        cmd.Parameters.Add(procParams[i]);
                    }
                }
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (conn != null)
                {
                    //conn.Dispose();
                    conn.Close();
                }
                if (adapter != null)
                {
                    adapter.Dispose();


                }
            }
            return ds;
        }
        /// <summary>
        /// </summary>
        /// <param name="paramName">param's name</param>
        /// <param name="value">values</param>
        /// <returns>SqlParameter</returns>
        public SqlParameter CreateParameter(string paramName, object value)
        {
            SqlParameter param = new SqlParameter(paramName, value);
            return param;
        }
        /// <summary>
        ///  get dataset by command and table
        /// </summary>
        /// <param name="command">query</param>
        /// <param name="Table">table</param>
        /// <returns>dataset</returns>
        public DataSet getdataset(String command, String Table)
        {
            DataSet ds = new DataSet();
            conn = new SqlConnection(GetConnectionString());
            conn.Open();
            SqlCommand sqlcom = new SqlCommand();
            SqlDataAdapter sqldatadapter = new SqlDataAdapter();
            sqlcom = conn.CreateCommand();
            sqlcom.CommandText = command;
            sqldatadapter.SelectCommand = sqlcom;
            sqldatadapter.Fill(ds, Table);
            return ds;
        }
        /// <summary>
        /// get user's id by command
        /// </summary>
        /// <param name="command">query</param>
        /// <returns>SqlDataReader</returns>
        public SqlDataReader getUserID(String command)
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
            conn = new SqlConnection(GetConnectionString());
            conn.Open();
            SqlCommand sqlcom = new SqlCommand();
            sqlcom = conn.CreateCommand();
            sqlcom.CommandText = command;
            SqlDataReader sqldatareader = sqlcom.ExecuteReader();

            return sqldatareader;
        }
        /// <summary>
        /// update into database
        /// </summary>
        /// <param name="command">query</param>
        public void UpdateField(String command)
        {
            conn = new SqlConnection(GetConnectionString());
            conn.Open();
            SqlCommand sqlcom = new SqlCommand();
            sqlcom = conn.CreateCommand();
            sqlcom.CommandText = command;
            sqlcom.ExecuteNonQuery();
            conn.Close();
        }

        public void CloseCon()
        {
            conn.Close();
        }
    }
}

